<template>
  <div>
    <el-dialog title="病历" v-model="innerVisible">
      <table style="width:100%;" v-loading="addLoading"
        element-loading-text="正在提交服务器中">
        <tr>
          <td></td><td></td><td></td>
          <td colspan=3>
            <el-input size="mini" placeholder="病 历 名 称 或 类 别" v-model="reports.name"></el-input>
          </td>
          <td></td><td></td>
        </tr>
        <tr>
          <td>医生:</td><td>{{ reports.doctor }}</td>
          <td>科别:</td><td>{{ reports.department }}</td>
          <td>医疗证:</td><td>{{ reports.doctor_licence }}</td>
          <td>时间:</td><td>{{ reports.d_time }}</td>
        </tr>
        <tr>
          <td>姓名:</td><td>{{ reports.partner_name }}</td>
          <td>性别:</td>
          <td>
            <div v-if="reports.sex == 'm'">男</div>
            <div v-if="reports.sex == 'w'">女</div>
          </td>
          <td>年龄:</td><td>{{ reports.age }}</td>
          <td>出生日期:</td><td>{{ reports.birthday }}</td>
        </tr>
        <tr>
          <td>联系电话:</td><td>{{ reports.phone }}</td>
          <td>地址:</td><td colspan=2>{{ reports.street }}</td>
          <td>过敏史:</td><td colspan=2>{{ reports.allergic_note }}</td>
        </tr>
        <tr>
          <td>主诉:</td><td colspan=5><el-input v-model="reports.main_description"></el-input></td>
          <td>诊断类型:</td>
          <td>
            <el-select v-model="reports.diagnostic_type" style="width:6em;">
              <el-option label="初诊" value="0"></el-option>
              <el-option label="复诊" value="1"></el-option>
            </el-select>
          </td>
        </tr>
        <tr>
          <td>现病史:</td>
          <td colspan=7>
            <el-input type="textarea" v-model="reports.nowill_description"></el-input>
          </td>
        </tr>
        <tr>
          <td>既往史:</td>
          <td colspan=7>
            <el-input type="textarea" v-model="reports.historyill_description"></el-input>
          </td>
        </tr>
        <tr>
          <td>个人史:</td>
          <td colspan=7>
            <el-input type="textarea" v-model="reports.personal_description"></el-input>
          </td>
        </tr>
        <tr>
          <td>家庭史:</td>
          <td colspan=7>
            <el-input type="textarea" v-model="reports.familyill_history"></el-input>
          </td>
        </tr>
      </table>
      <span slot="footer" class="dialog-footer">
        <el-button @click.native="innerVisible=false">取 消</el-button>
        <el-button type="primary" @click.native="addReport(reports)">{{ buttonName }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import '../../assets/scripts/TimeFormat.js'
export default {
  data() {
    return {
      reports: {
        id: 0,
        prescription_type: '',
        name: '',
        doctor: '医生类',
        department: '科别类',
        doctor_licence: '123456789',
        d_time: null,
        partner_id: '',
        partner_name: '',
        sex: '',
        age: '',
        birthday: '',
        phone: '',
        street: '',
        allergic_note: [],
        main_description: '',
        diagnostic_type: '0',
        nowill_description: '',
        historyill_description: '',
        personal_description: '',
        familyill_history: ''
      },
      innerVisible: false,
      currentTime: null,
      buttonName: '',
      addLoading: false
    }
  },
  props: {
    judge: null,
    partnerId: null,
    Visible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    addReport(reports) {
      this.addLoading = true
      var str = JSON.stringify(reports)
      var data = JSON.parse(str)
      console.log(data)
      if (this.judge == 'add') {
        this.$http.post('/api/medicalRecords/medicineRecord', data).then((response) => {
          this.addLoading = false
          this.innerVisible = false
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          })
        }, (response) => {
          this.addLoading = false
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
        })
      }
      if (this.judge == 'detailed') {
        if (this.currentTime == data.d_time.substring(0,10)) {
          this.$http.post('/api/medicalRecords/medicineRecord/up', data).then((response) => {
            this.addLoading = false
            this.innerVisible = false
            this.$message({
              showClose: true,
              message: '成功修改',
              type: 'success'
            })
          }, (response) => {
            this.addLoading = false
            this.$message({
              showClose: true,
              message: '服务器连接失败',
              type: 'error'
            })
          })
        } else {
          this.addLoading = false
          this.$alert('不能修改今天以外的病历', '提示', {
            type: 'warning',
            callback: action => {
              let vm = this
              vm.$http.get('/api/medicalRecords/'+vm.partnerId).then(function (response) {
                let date = new Date(response.headers.map.date[0])
                let data = response.data[0]
                vm.currentTime = date.Format("yyyy-MM-dd")
                vm.reports = Object.assign({},data)
                vm.reports.partner_id = data.partner_id
                vm.reports.partner_name = data.partner_name
                if (!vm.reports.prescription_type) {
                  vm.reports.prescription_type = ''
                }
                if (!vm.reports.allergic_history) {
                  vm.reports.allergic_history = ''
                }
              })
            }
          })
        }
      }
    }
  },
  watch: {
    Visible: function (val, oldVal) {
      this.innerVisible = val
    },
    innerVisible: function (val, oldVal) {
      if (val) {
        let vm = this
        if (vm.judge == 'add') {
          vm.buttonName = '添加'
          vm.$http.get('/api/patients/'+vm.partnerId).then(function(response) {
            let date = new Date(response.headers.map.date[0])
            let data = response.data
            vm.reports.d_time = date.Format("yyyy-MM-dd")
            vm.reports.partner_id = data.base.id
            vm.reports.partner_name = data.base.name
            vm.reports.sex = data.base.sex
            vm.reports.age = data.base.age
            vm.reports.birthday = data.base.birthday
            vm.reports.phone = data.base.phone
            vm.reports.street = data.base.street
            vm.reports.allergic_note = data.allergic.map(function(obj) {
              return obj.drug_allergic
            }).join(', ')
          }, (response) => {
            vm.$message({
              showClose: true,
              message: '服务器连接失败',
              type: 'error'
            })
          })
        }
        if (vm.judge == 'detailed') {
          vm.buttonName = '修改'
          vm.$http.get('/api/medicalRecords/'+vm.partnerId).then(function (response) {
            let date = new Date(response.headers.map.date[0])
            let data = response.data[0]
            vm.currentTime = date.Format("yyyy-MM-dd")
            vm.reports = Object.assign({},data)
            vm.reports.partner_id = data.partner_id
            vm.reports.partner_name = data.partner_name
            if (!vm.reports.prescription_type) {
              vm.reports.prescription_type = ''
            }
            if (!vm.reports.allergic_history) {
              vm.reports.allergic_history = ''
            }
          })
        }
      } else {
        this.$emit('fromson')
      }
    }
  }
}
</script>